﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.Web.Script.Serialization;
using System.Linq;

namespace WebAppLibrary.GetProvCity
{
    [Serializable]
    public class Area : IArea<Area>
    {
        /// <summary>
        /// Gets the DOM.
        /// </summary>
        /// <value>The DOM.</value>
        private XmlDocument Dom
        {
            get
            {
                XmlDocument doc = new XmlDocument();
                string path = HttpContext.Current.Server.MapPath("/XMLData/Area.xml");
                doc.Load(path);
                return doc;
            }
        }

        /// <summary>
        /// _name
        /// </summary>
        private string _name;
        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return _name; }
        }

        #if DEBUG
        public 
        #else
            internal
        #endif
        Area()
        {
            ;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Area"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        private Area(string name)
        {
            this._name = name;
        }

        /// <summary>
        /// Gets the node list.
        /// </summary>
        /// <param name="xpath">The xpath.</param>
        /// <returns></returns>
        private XmlNodeList GetNodeList(string xpath)
        {
            return this.Dom.SelectNodes(xpath);
        }
        /// <summary>
        /// Gets the area.
        /// </summary>
        /// <param name="nList">The n list.</param>
        /// <returns></returns>
        private List<Area> GetArea(XmlNodeList nList)
        {
            List<Area> areas = new List<Area>();
            foreach (XmlNode node in nList)
                areas.Add(new Area(node.Attributes["name"].InnerText));
            return areas;
        }

        /// <summary>
        /// Gets the prov.
        /// </summary>
        /// <returns></returns>
        public List<Area> GetProv()
        {
            XmlNodeList nodelist = this.GetNodeList("//address/province");
            if (nodelist.Count == 0)
                throw new Exception("data is null");
            return this.GetArea(nodelist);
        }

        /// <summary>
        /// Gets the prov.
        /// </summary>
        /// <param name="provname">The provname.</param>
        /// <returns></returns>
        [Obsolete]
        public List<Area> GetProv(string provname)
        {
            return this.GetProv().FindAll(new Predicate<Area>(FindProv));
        }
        /// <summary>
        /// Finds the prov.
        /// </summary>
        /// <param name="ara">The ara.</param>
        /// <returns></returns>
        private bool FindProv(Area ara)
        {
            if (ara.Name == this.Name)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Gets the city.
        /// </summary>
        /// <param name="provname">The provname.</param>
        /// <returns></returns>
        public List<Area> GetCity(string provname)
        {
            string xpath = string.Format("//address/province[@name='{0}']/city", provname);
            return this.GetArea(this.GetNodeList(xpath));
        }
        /// <summary>
        /// Gets the country.
        /// </summary>
        /// <param name="cityname">The cityname.</param>
        /// <returns></returns>
        public List<Area> GetCountry(string cityname)
        {
            string xpath = string.Format("//address/province/city[@name='{0}']/country", cityname);
            return GetArea(this.GetNodeList(xpath));
        }
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.Dom.OuterXml;
        }

        #region IArea<Area> 成员


        /// <summary>
        /// Gets the prov.
        /// </summary>
        /// <returns></returns>
        IList<Area> IArea<Area>.GetProv()
        {
            return this.GetProv();
        }

        /// <summary>
        /// Gets the city.
        /// </summary>
        /// <param name="provname">The provname.</param>
        /// <returns></returns>
        IList<Area> IArea<Area>.GetCity(string provname)
        {
            return this.GetCity(provname);
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        string IArea<Area>.Name
        {
            get { return this._name; }
        }

        /// <summary>
        /// Gets the area.
        /// </summary>
        /// <param name="cityname">The cityname.</param>
        /// <returns></returns>
        IList<Area> IArea<Area>.GetArea(string cityname)
        {
            return this.GetCountry(cityname);
        }
        #endregion
    }
}
